// pages/zhuanjiemaerweiye/zhuanjiemaerweiye.js
const app = getApp();
import QRCode from '../../utils/weapp-qrcode';
Page({

  /**
   * 页面的初始数据
   */
  data: {

 

    wang_d: '', //地点
    city: [], //市
    district: [], //区
    business: '', //网点
    shi_k: true,
    qu_k: true,
    wang_k: true,
    tishi: true,



    shiquxuan: true,

    L2name: '',
    L3name: '',
    L4name: '',

    L4code: '',
  },

  region() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2029_outletPush',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          city: res.data.data,
        })

      }
    })
  },


  but_shi() {
    this.setData({
      shi_k: false,

    })

    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });

    console.log(this.data.session_key);
  },

  but_xian() {
    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择市',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        qu_k: false
      })
    }
  },

  but_w() {

    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择区',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        wang_k: false
      })
    }

  },


  //市
  buttn_c(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.district);
    console.log(e.currentTarget.dataset.item.L2code);
    this.setData({
      district: e.currentTarget.dataset.item.district,
      L2code: e.currentTarget.dataset.item.L2code,
      L2name: e.currentTarget.dataset.item.L2name,
      shi_k: true,
      L3name: '',
      L4name: '',

    })

  },

  buttn_x(e) {
    console.log(e);


    this.setData({
      L3code: e.currentTarget.dataset.item.L3code,
      L3name: e.currentTarget.dataset.item.L3name,
      qu_k: true,
      L4name: '',
    })
    this.Outlets()

  },


  buttn_w(e) {
    console.log(e);
    this.setData({
      L4name: e.currentTarget.dataset.item.L4name,
      L4code: e.currentTarget.dataset.item.L4code,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,

      wang_k: true
    })





  },

  //网点
  Outlets() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2027_outletList',
      method: 'POST',
      data: {
        L2code: this.data.L2code,
        L3code: this.data.L3code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.data);
        this.setData({
          business: res.data.data
        })

      }
    })
  },




  //网点
  bouut() {
    wx.request({
      url: app.globalData.interface+'/crossVersionOne/API2_2131_UpdateUserBrch',
      method: 'POST',
      data: {

        OPEN_ID: this.data.OPEN_ID, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami,
        L4code: this.data.L4code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        let that =this
        wx.showModal({
          title: '温馨提示',
          content: res.data.data,
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后
              console.log('用户点击确定')
              that.setData({
                shiquxuan: true
              })
              that.sihuodong()
            } else { //这里是点击了取消以后
              console.log('用户点击取消')
              that.sihuodong()
              that.setData({
                shiquxuan: true
              })
            }
          }
        })
      }
    })
  },











  

  // https://www.xajiarong.net/zhuanjiema?id=1&name=5

  sihuodong() {


    this.setData({
      EMPLOYEE_ID: app.globalData.mealsList,
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,


    })

    console.log(app.globalData.jiami);
    wx.request({
      url: app.globalData.interface + '/management/API3_1011_SelectZhuanJieRenForMe',
      method: 'POST',
      data: {

        OPEN_ID: app.globalData.openid, //用户OPEN_ID
        session_key: app.globalData.session_key,
        OPEN_UUID: app.globalData.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data);
let that =this

        if (res.data.result == 'error') {

          if (res.data.type == 1 ) {
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  that.setData({
                    shiquxuan: false
                  })
              
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                  that.setData({
                    shiquxuan: false
                  })
                }
              }
            })
          }else{
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          }
        } else {
          this.setData({
            txtlist: res.data.data,
            // ajaj: 'CUSTOMER_ID=' + app.globalData.CUSTOMER_ID + '&' + 'openid=' + app.globalData.openid,
            ajaj: 'https://www.xajiarong.net/jiexizhuanjiema?' + 'id=' + res.data.data.L4code + '&' + 'name=' + res.data.data.OPEN_ID 
          })
          setTimeout(() => {
            wx.showLoading({
              title: '正在生成...'
            });
            this.erweima()

          }, 500)

        }






      }
    })
  },




  erweima() {




    this.setData({
      openid: app.globalData.openid,
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,

    });


    console.log(this.data.ajaj);
    new QRCode('myQrcode', {
      text: this.data.ajaj,
      // text:'aaaa',
      width: 121, //canvas 画布的宽
      height: 121, //canvas 画布的高
      padding: 0, // 生成二维码四周自动留边宽度，不传入默认为0
      margin: 0,
      correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
      callback: (res) => {
        //工具回调数据
        // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
        wx.hideLoading()
        console.log("生成二维码", res)
        //将图片路劲放入data中，显示在wxml的image标签上
        this.setData({
          ewmImg: res.path,
          // showEwmIndex: e.currentTarget.dataset.index
        })

      }
    })
  },





  /**
   * 生命周期函数--监听页面加载
   */
  // getUrlParams: function(url) {
  //   const params = {};
  //   const queryString = url.includes('?') ? url.split('?')[1] : '';
    
  //   if (queryString) {
  //     queryString.split('&').forEach(pair => {
  //       const [key, value] = pair.split('=');
  //       params[key] = value || '';
  //     });
  //   }
    
  //   return params;
  // },
  onLoad(options) {

    // const qrUrl = decodeURIComponent(options.q);
    // console.log("二维码链接:", qrUrl);
    
    // // 提取参数
    // const urlParams = this.getUrlParams(qrUrl);
    // console.log("解析后的参数:", urlParams)
    // console.log(urlParams.id);
    // console.log(urlParams.name);


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({

      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })
    // this.sihuodong()


    setTimeout(() => {

      this.sihuodong()

    }, 800)

    wx.hideShareMenu();


    this.region()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})